package com.best.service;

import com.best.dao.UserDao;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * @author Jiang Akang
 * employeeId: BG435424
 * @date 2021/6/5
 **/
@Service
public class UserService {

    //注入dao
    @Autowired
    private UserDao userDao;

    //转账的方法
    @Transactional(rollbackFor = Exception.class)
    public void accountMoney() {
        try {
            // 第一步 开启事务
            // 第二步 进行业务操作
            // lucy少100
            userDao.reduceMoney();
            // 模拟异常 NullPointerException
             String str = null;
             str.toLowerCase();
            // mary多100
            userDao.addMoney();
            UserService userService = (UserService) AopContext.currentProxy();
            userService.testTransaction();
            // 第三步 没有发送异常，提交事务
        } catch (Exception e) {
            // 第四步 出现异常，事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
    public void testTransaction() {}

}